<template>
		<view class="body" :style="{background: showBgImg ? `url(${listInfo.img})no-repeat 10% 10%` : '', backgroundColor: 'rgba(117, 117, 117, 0.03);'}">
			<scroll-view scroll-y="true" class="scroll-Y" :enable-back-to-top="true">
				<!-- 标题栏 -->
				<view class="header">
					<view class="listInfoWrap">
						<view class="imgWrap">
							<view class="playCount">
								<i style="position: absolute;top: 0;right: 0;" class="iconfont icon-bofang1">{{listInfo.playcount | playCountFormat}}</i>
							</view>
							<image :src="listInfo.img" mode="scaleToFill"></image>
						</view>
						<view class="listInfo">
							<text class="listName">{{listInfo.name}}</text>
							<view class="listCreator">
								<image :src="listInfo.creatoravatarUrl" mode="" class="creatorImg"></image>
								<text>{{listInfo.creatorname}}</text>
							</view>
							<view class="descWrap" @tap="openSheetInfoPage(listInfo)">
								<view class="desc">
									<text>{{listInfo.description}}</text>
								</view>
								<view class="descIcon">
									<i style="font-size: 14px;" class="iconfont icon-you"></i>
								</view>
							</view>
						</view>
					</view>
					<button plain="true" open-type="share" class="share" @tap="openSheetInfoPage()">
						<i class='iconfont icon-fenxiang'>分享给微信好友</i>
					</button>
				</view>
				
				<!-- 列表区 -->
				<view class="songlist">
					<view class="playAll" @tap="playAllSongs()">
						<i class="iconfont icon-media-play-03" style="margin-right:35rpx;font-size:50rpx;"></i>
						<view class="playAllText">播放全部<text class="playAllNumber">({{currentSongList.length}})</text></view>
					</view>
					<view class="list-item" @tap="playSong(index)" v-for="(item, index) in currentSongList" :key="item.id" :style="{marginBottom: index === currentSongList.length-1? '60rpx':''}">
						<view class="numberWrap">
							<view class="numberBadge" >{{index + 1}}</view>
						</view>
						<view class="songInfo">
							<view class="nameWrap">
								<i v-if="curPlaySymbol === index" style="color: #313131;margin-right: 10rpx;" class="iconfont icon-playing-copy"></i>
								<view class="songname">{{item.name}}</view>
							</view>
							<view class="descWrap">
								<view class="description">{{item.singer}}—{{item.sename}}</view>
							</view>
						</view>
						<view class="playBtn">
							<i :class="item.icon"></i>
						</view>
					</view>
				</view>
				
				
			</scroll-view>
		</view>
</template>

<script>
	import { mapMutations, mapState } from	'vuex'
	export default {
		data() {
			return {
				showBgImg: false,
				listInfo: null,
				currentDate: '',
				currentMonth: '',
				playIcon: 'iconfont icon-media-play-03',
				stopIcon: 'iconfont icon-zantingtingzhi1',
				currentPlaySymbol: null,
				currentSongList: null
			}
		},
		computed:{
			...mapState(['curTapIndex', 'globalPlayList', 'curMusicInfo', 'globalPlayStatus']),
			curPlaySymbol(){
				this.currentPlaySymbol = this.curTapIndex
				return this.currentPlaySymbol
			}
		},
		methods: {
			...mapMutations(['updateGlobalSheetSongListInfo', 'changeGlobalPlayList', 'changeCurMusicInfo', 'changeCurTapIndex', 'changePlayStatus', 'changeStopStatus']),
			playSong(index) {
				let musicInfo = this.currentSongList[index]
				this.changeCurMusicInfo(musicInfo)
				this.changeCurTapIndex(index)
				this.changePlayStatus()
				this.$player.title = musicInfo.name
				this.$player.singer = musicInfo.singer
				this.$player.coverImgUrl = musicInfo.picurl
				this.$player.src = `https://music.163.com/song/media/outer/url?id=${musicInfo.mid}.mp3`
				this.openPlayPage(musicInfo.mid)
			},
			playAllSongs() {
				let musicInfo = this.currentSongList[0]
				let mid = musicInfo.mid
				this.changeCurMusicInfo(musicInfo)
				this.changeCurTapIndex(0)
				this.changePlayStatus()
				this.currentPlaySymbol = 0
				this.$player.title = musicInfo.name
				this.$player.singer = musicInfo.singer
				this.$player.coverImgUrl = musicInfo.picurl
				this.$player.src = `https://music.163.com/song/media/outer/url?id=${mid}.mp3`
				this.openPlayPage(mid)
			},
			openPlayPage(id) {
				uni.navigateTo({
					url: `../play/play?mid=${id}`
				})
			},
			openSheetInfoPage(sheetInfo) {
				uni.navigateTo({
					url: `../songSheetInfo/songSheetInfo`
				})
			}
		},
		onLoad(option) {
			setTimeout(()=>{
				this.showBgImg = true
			},800)
			this.changeCurTapIndex(null) // 将当前的index置空
			let that = this
			
			let userInfo = uni.getStorageSync('userInfo')
			if(userInfo) {
				uni.request({
					url: `${that.$http.vercelUrl}/playlist/detail?id=${option.listId}`,
					header:{
						cookie: userInfo.cookie
					},
					success(res1) {
						console.log('已登录歌单信息：',res1.data);
						let trackList = res1.data.playlist.tracks		// 部分歌单信息
						let trackIds = res1.data.playlist.trackIds	// 完整歌单信息
						that.listInfo = {
							img: res1.data.playlist.coverImgUrl,
							name: res1.data.playlist.name,
							creatorname: res1.data.playlist.creator.nickname,
							creatoravatarUrl: res1.data.playlist.creator.avatarUrl,
							playcount: res1.data.playlist.playCount,
							description: res1.data.playlist.description,
							tags: res1.data.playlist.tags
						}
						console.log('that.listInfo', that.listInfo);
						that.updateGlobalSheetSongListInfo(that.listInfo)
						if (trackList.length <= 500) {
							// 初始链接
							let reqUrl = `${that.$http.vercelUrl}/song/detail?ids=`
							for (let i = trackList.length; i < trackIds.length; i++) {
								reqUrl += `${trackIds[i].id},`
							}
							// 添加完ID后的链接
							let correctUrl = reqUrl.substring(0, reqUrl.length-1)
							// console.log(correctUrl);
							uni.request({
								url: correctUrl,
								success(res2) {
									// console.log('trackIds后续歌曲信息：',res2.data.songs);
									that.currentSongList = []
									trackList.forEach(function(item, index){
										that.currentSongList[index] = {
											id: index,								// keyId
											name: item.name,					// 音乐名
											sename: item.al.name, 		// 专辑名
											singer: item.ar[0].name,	// 歌手名
											mid: item.id,							// 歌曲id(用于获取对应的播放url)
											icon: 'iconfont icon-media-play-03', // 设置播放字体图标,以控制对应歌曲的播放按钮图标
											picurl: item.al.picUrl
										}
									})
									res2.data.songs.forEach(function(item, index){
										that.currentSongList.push({
											id: (index + that.currentSongList.length),								// keyId
											name: item.name,					// 音乐名
											sename: item.al.name, 		// 专辑名
											singer: item.ar[0].name,	// 歌手名
											mid: item.id,							// 歌曲id(用于获取对应的播放url)
											icon: 'iconfont icon-media-play-03', // 设置播放字体图标,以控制对应歌曲的播放按钮图标
											picurl: item.al.picUrl
										})
									})
									// console.log('currentSongList:',that.currentSongList);
									that.changeGlobalPlayList(that.currentSongList)
								}
							})
						} else {
							that.currentSongList = []
							res1.data.tracks.forEach(function(item, index){
								that.currentSongList[index] = {
									id: index,								// keyId
									name: item.name,					// 音乐名
									sename: item.al.name, 		// 专辑名
									singer: item.ar[0].name,	// 歌手名
									mid: item.id,							// 歌曲id(用于获取对应的播放url)
									icon: 'iconfont icon-media-play-03', // 设置播放字体图标,以控制对应歌曲的播放按钮图标
									picurl: item.al.picUrl
								}
							})
							that.changeGlobalPlayList(that.currentSongList)
						}
					},
					fail(e) {
						console.log('获取登录歌单信息失败：', e);
					}
				})
			} else {
				uni.request({
					url: `${that.$http.vercelUrl}/playlist/detail?id=${option.listId}`,
					success(res) {
						// console.log('未登录歌单信息：',res.data);
						// let recommendInfoList = res.data.result
						// that.recommendList = []
						// recommendInfoList.forEach((item, index, array) => {
						// 	that.recommendList[index] = {
						// 		listId: array[index].id,
						// 		listName: array[index].name,
						// 		picUrl: array[index].picUrl,
						// 		playCount: array[index].playCount
						// 	}
						// })
						// console.log('登录推荐歌单(已处理)：', that.recommendList);
					},
					fail(e) {
						console.log('获取未登录歌单信息失败：', e);
					}
				})
			}
			
		},
		onShow() {
		}
	}
</script>

<style lang="scss" scoped>

.body {
	width: 100%;
	height: 100%;
	position: absolute;
	background-size: cover;
	.scroll-Y {
		height: 100%;
		background: rgba(59, 59, 59, 0.5);
		backdrop-filter: blur(30px);
		.header {
			width: 100%;
			height: 35%;
			position: relative;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			.listInfoWrap {
				display: flex;
				justify-content: flex-start;
				align-items: center;
				position: relative;
				.imgWrap {
					width: 40%;
					height: 98%;
					position: relative;
					border-radius: 20px;
					overflow: hidden;
					margin: 0 40rpx;
					display: flex;
					justify-content: center;
					align-items: center;
					.playCount {
						width: 110rpx;
						display: flex;
						justify-content: center;
						align-items: center;
						position: absolute;
						top: 6rpx;
						right: 6rpx;
						color: #FFFFFF;
						height: 36rpx;
						background-color: rgba(124, 113, 113, 0.68);
						border-radius: 6px;
						.iconfont {
							font-size: 26rpx;
						}
					}
					image {
						width: 140px;
						height: 140px;
					}
				}
				.listInfo {
					display: flex;
					flex-direction: column;
					justify-content: flex-start;
					align-items: flex-start;
					width: 60%;
					.listName {
						font-weight: 600;
						font-size: 30rpx;
						color: #fff;
					}
					.listCreator {
						width: 98%;
						display: flex;
						justify-content: flex-start;
						align-items: center;
						margin: 15px auto;
						image {
							width: 50rpx;
							height: 50rpx;
							border-radius: 20px;
							margin-right: 10rpx;
						}
						text {
							color: #FFFFFF;
							font-size: 28rpx;
						}
					}
					.descWrap {
						display: flex;
						justify-content: flex-start;
						align-items: center;
						font-size: 28rpx;
						.desc {
							width: 280rpx;
							text-overflow: ellipsis;
							white-space: nowrap;
							overflow: hidden;
							height: 20px;
							color: #FFFFFF;
						}
						.descIcon {
							color: #FFFFFF;
							margin-left: 10rpx;
							line-height: 40rpx;
						}
					}
					
				}
			}
			.share {
				margin: 10rpx auto;
				width: 50%;
				height: 70rpx;
				border-radius: 30px;
				background-color: rgba(0, 0, 0, 0.2);
				display: flex;
				justify-content: center;
				align-items: center;
				letter-spacing: 1px;
				margin-top: 30px;
				color: #fff;
				border: none;
			}
		}
		
		.songlist {
			background: #FFFFFF;
			border-radius: 25px 25px 0 0;
			.playAll {
				height: 90rpx;
				display: flex;
				justify-content: flex-start;
				align-items: center;
				margin-left: 30rpx;
				color: #313131;
				iconfont {
					color: #313131;
				}
				.playAllText {
					font-size: 37rpx;
					.playAllNumber {
						font-size: 31rpx;
					}
				}
			}
			.list-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				height: 130rpx;
				.numberWrap {
					width: 10%;
					display: inline-block;
					text-align: center;
					.numberBadge {
						color: #959595;
						padding-left: 30rpx;
						font-size: 36rpx;
					}
				}
				.songInfo {
					width: 60%;
					display: inline-block;
					.nameWrap {
						display: flex;
						justify-content: flex-start;
						.songname {
							font-size: 33rpx;
							font-weight: 500;
							white-space: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;
							color: #292929;
						}
					}
					.descWrap {
						display: flex;
						justify-content: flex-start;
						.description {
							white-space: nowrap;
							text-overflow: ellipsis;
							overflow: hidden;
							font-size: 24rpx;
							color: #979797;
						}
					}
				}
				.playBtn {
					width: 20%;
					display: inline-block;
					.iconfont {
						color: #cacaca;
						font-size: 56rpx;
						margin-left: 40rpx;
					}
				}
			}
		}
	}
	
}




</style>
